package lab03;

import com.sun.org.apache.xpath.internal.SourceTree;

import java.io.*;
import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
        //创建文件
       File file = new File("E://a.txt");
       //判断文件是否存在
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //键盘录入需要加密的字符
        System.out.println("请输入需要加密的字符：");
        Scanner sc = new Scanner(System.in);
        String string = sc.next();

        //用数组来存放需要加密的字符
        char[] a = new char[string.length()];
        char[] b = new char[string.length()];
        a = string.toCharArray();
        //读文件开始加密
        try {
            FileWriter writer = new FileWriter(file);
            int i = 0;
            for (i = 0;i<string.length() ; i++){
                //把字符用数组来存放需要用到强制转化
                int c = (int) (a[i] +1);
                b[i] = (char) c;
                System.out.println(b[i]);

            }
                //用完后要关闭
            writer.write(b);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //解密文件
        System.out.println("解密后：");
        FileReader reader = null;
        try {
          reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int m = 0;
        try {
            while ((m = reader.read())!= -1)
                System.out.println((char)( m-1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
